package com.lyf.baoxian.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lyf.baoxian.R;
import com.lyf.baoxian.fragment.BaoDanFragment;
import com.lyf.baoxian.fragment.CustomerFragment;
import com.lyf.baoxian.fragment.HomeFragment;
import com.lyf.baoxian.fragment.MyFragment;
import com.lyf.baoxian.fragment.TaoCanFragment;
import com.lyf.baoxian.fragment.TouBaoFragment;

/**
 * Created by 刘亚飞 on 2017/2/9.
 */

public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout ll_shouye, ll_baodan, ll_kehu, ll_nongxian, ll_wode;
    private ImageView iv1, iv2, iv3, iv4, iv5;
    private TextView tv1, tv2, tv3, tv4, tv5;

    private HomeFragment homeFragment;
    private TaoCanFragment taoCanFragment;
    private MyFragment myFragment;
    private CustomerFragment customerFragment;
    private BaoDanFragment baoDanFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        initView();
        initClick();
        initFragment(0);
        //默认选中首页
        setColor(tv1, iv1, Color.WHITE, R.drawable.shouye);

    }


    /**
     * 初始化布局
     */
    private void initView() {
        ll_shouye = (LinearLayout) findViewById(R.id.ll_shouye);
        ll_baodan = (LinearLayout) findViewById(R.id.ll_baodan);
        ll_kehu = (LinearLayout) findViewById(R.id.ll_kehu);
        ll_nongxian = (LinearLayout) findViewById(R.id.ll_nongxian);
        ll_wode = (LinearLayout) findViewById(R.id.ll_wode);

        iv1 = (ImageView) findViewById(R.id.iv1);
        iv2 = (ImageView) findViewById(R.id.iv2);
        iv3 = (ImageView) findViewById(R.id.iv3);
        iv4 = (ImageView) findViewById(R.id.iv4);
        iv5 = (ImageView) findViewById(R.id.iv5);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tv3 = (TextView) findViewById(R.id.tv3);
        tv4 = (TextView) findViewById(R.id.tv4);
        tv5 = (TextView) findViewById(R.id.tv5);
    }

    /**
     * 初始化点击事件
     */
    private void initClick() {

        ll_shouye.setOnClickListener(this);
        ll_baodan.setOnClickListener(this);
        ll_kehu.setOnClickListener(this);
        ll_nongxian.setOnClickListener(this);
        ll_wode.setOnClickListener(this);

    }

    /**
     * 初始化frgament
     *
     * @param index
     */
    private void initFragment(int index) {
        //Fragment管理器
        FragmentManager manager = getSupportFragmentManager();
        //开启事物
        FragmentTransaction transaction = manager.beginTransaction();
        //隐藏所有的fragment
        hide(transaction);
        switch (index) {
            case 0: //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fl_container, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 1: //保单
                if (baoDanFragment == null) {
                    baoDanFragment = new BaoDanFragment();
                    transaction.add(R.id.fl_container, baoDanFragment);
                } else {
                    transaction.show(baoDanFragment);
                }
                break;
            case 2: //客户
                if (customerFragment == null) {
                    customerFragment = new CustomerFragment();
                    transaction.add(R.id.fl_container, customerFragment);
                } else {
                    transaction.show(customerFragment);
                }
                break;
            case 3: //农险
                if (taoCanFragment == null) {
                    taoCanFragment = new TaoCanFragment();
                    transaction.add(R.id.fl_container, taoCanFragment);
                } else {
                    transaction.show(taoCanFragment);
                }
                break;
            case 4: //我的
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.fl_container, myFragment);
                } else {
                    transaction.show(myFragment);
                }
                break;
        }

        transaction.commit();

    }


    /**
     * 隐藏所有的事物
     *
     * @param transaction
     */
    private void hide(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }

        if (baoDanFragment != null) {
            transaction.hide(baoDanFragment);
        }
        if (customerFragment != null) {
            transaction.hide(customerFragment);
        }
        if (taoCanFragment != null) {
            transaction.hide(taoCanFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_shouye:  //首页
                clearColor();
                setColor(tv1, iv1, Color.WHITE, R.drawable.shouye);
                initFragment(0);
                break;

            case R.id.ll_baodan:  //保单
                clearColor();
                setColor(tv2, iv2, Color.WHITE, R.drawable.baodan);
                initFragment(1);
                break;

            case R.id.ll_kehu:  //客户
                clearColor();
                setColor(tv3, iv3, Color.WHITE, R.drawable.kehu);
                initFragment(2);
                break;

            case R.id.ll_nongxian:  //农险
                clearColor();
                setColor(tv4, iv4, Color.WHITE, R.drawable.nongxian);
                initFragment(3);
                break;

            case R.id.ll_wode:  //我的
                clearColor();
                setColor(tv5, iv5, Color.WHITE, R.drawable.wode);
                initFragment(4);
                break;
        }

    }


    /**
     * 设置选中底部的颜色
     *
     * @param tv
     * @param iv
     * @param color
     * @param image
     */
    private void setColor(TextView tv, ImageView iv, int color, int image) {
        iv.setImageResource(image);
        tv.setTextColor(color);
    }

    /**
     * 选中底部时候，先清除之前选择的布局
     */
    private void clearColor() {
        iv1.setImageResource(R.drawable.shouye1);
        tv1.setTextColor(getResources().getColor(R.color.home_tab_tv));

        iv2.setImageResource(R.drawable.baodan1);
        tv2.setTextColor(getResources().getColor(R.color.home_tab_tv));

        iv3.setImageResource(R.drawable.kehu1);
        tv3.setTextColor(getResources().getColor(R.color.home_tab_tv));

        iv4.setImageResource(R.drawable.nongxian1);
        tv4.setTextColor(getResources().getColor(R.color.home_tab_tv));

        iv5.setImageResource(R.drawable.wode1);
        tv5.setTextColor(getResources().getColor(R.color.home_tab_tv));
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(MainActivity1.this).setTitle("真的要离开")
                    .setMessage("确定要离开")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            MainActivity1.this.finish();

                        }
                    }).setNegativeButton("取消", null).show();

        }
        return super.onKeyDown(keyCode, event);
    }
}
